|
|
|
|
|
|
|
The final important function used in this process is the PointInside function. This function determines whether a point lies within the projection of a polygon. It does this by adding up the angles between the lines drawn from the point to each vertex of the polygon. If the point lies outside the polygon, the sum is zero. If the point lies inside the polygon, the sum will be 2 * p or 2 * p, depending on the direction in which you examine the vertices. |
|
|
|
|
|
|
|
|
To see why this works, imagine running a line segment from the point to one of the vertices in the polygon. Then rotate the line segment to the next vertex. Continue rotating the segment to visit the vertices in order around the polygon's edges. |
|
|
|
|
|
|
|
|
Suppose the point lies far to the left of the polygon. Then the segment will move up and down, making various angles with the vertices. The positive angles are canceled by the negative angles, so you finish where you started and the total sum of the angles is zero. |
|
|
|
|
|
|
|
|
Now suppose the point is in the middle of the polygon. Then as you rotate the segment through the vertices, the segment rotates 360 degrees around the point. It finishes where it started, but it has made a loop around the point. That makes the sum of the angles between the segment's positions add up to 360 or 360 degrees. |
|
|
|
 |
|
|
|
|
' Return True if the point projection lies within
' this polygon's projection. Public Function PointInside( _
ByVal X As Single, ByVal Y As Single) As Boolean
Dim i As Integer
Dim theta1 As Double
Dim theta2 As Double
Dim dtheta As Double
Dim dx As Double
Dim dy As Double
Dim angles As Double
dx = Points(NumPts).trans(1) - X
dy = Points(NumPts).trans(2) - Y
theta1 = ATan2(CSng(dy), CSng(dx))
If theta1 < 0 Then theta1 = theta1 + 2 * PI
For i = 1 To NumPts
dx = Points(i).trans(1) - X
dy = Points(i).trans(2) - Y
theta2 = ATan2(CSng(dy), CSng(dx))
If theta2 < 0 Then theta2 = theta2 + 2 * PI
dtheta = theta2 - theta1
If dtheta > PI Then dtheta = dtheta - 2 * PI
If dtheta < -PI Then dtheta = dtheta + 2 * PI
angles = angles + dtheta
theta1 = theta2
Next i
PointInside = (Abs(angles) > 0.001)
End Function |
|
|
|
|
|